<template>
    <div>
        <h4 class="font-weight-bold">
            <span class="text-muted font-weight-light">工具执行结果 /</span> 执行批次
        </h4>
        <b-card class="mb-4">
            <div>
                <Table :data="table.data" :columns="table.columns" :loading="table.loading" stripe border class="mt-2"></Table>
                <Page show-elevator show-total size="small" class="mt-2" :total="table.total" :current="table.currentPage"
                        @on-change="(pageNum) => {
                            let self = this;
                            table.pageChange.call(self,pageNum);
                        }"></Page>
            </div>
        </b-card>
    </div>
</template>

<style scoped>
</style>

<script>
function padLeftZero (str) {
    return ('00' + str).substr(str.length);
};
function formatDate (longStr, fmt) {
   let date = new Date(longStr);

    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};
export default {
    name: "finalResultToolScorecard",
    metaInfo: {
        title: "工具执行结果 - 打分卡结果"
    },
    data() {
        return {
            table: {
                showIndex: false,
                data: [],
                columns: [
                    {
                        title: "批次号",
                        key: "batchNo"
                    },
                    {
                        title: "执行方式",
                        key: "runType",
                        render: (h, params) => {
                            let runTypeName;
                            if(params.row.runType == 'manual'){
                                runTypeName = '手动执行';
                            } else if(params.row.runType == 'job'){
                                runTypeName = 'Job';
                            } else if(params.row.runType == 'syscall'){
                                runTypeName = '外部调用';
                            }
                            return (
                                <div>
                                    {runTypeName}
                                </div>
                            );
                        }
                    },
                    {
                        title: "执行开始时间",
                        key: "createDate",
                        render: (h, params) => {
                            let createDate = formatDate(params.row.createDate,'yyyy-MM-dd hh:mm:ss');
                            return (
                                <div>
                                    {createDate}
                                </div>
                            );
                        }
                    }
                ],
                // 是否载入中
                loading: false,
                // 总页数
                total: 0,
                // 当前页数
                currentPage : 1,
                // 每页条目数
                pageSize : 10,
                // 分页切换
                pageChange: function(pageNum){
                    let self = this;
                    self.table.currentPage = pageNum;
                    self.loadTable().then(function(res){
                        if(res.data){
                            self.table.data = res.data.data ? res.data.data : [];
                        }
                    });
                }
            }
        };
    },
    // created() {
    //     this.$http
    //         .get("/app/v1/toolRunLogs/batchNos")
    //         .then(res => {
    //             console.log(res.data.data)
    //             this.table.data = res.data.data;
    //         });
    // },
    mounted(){
        let self = this;
        //加载客户列表
        self.loadTable().then(function(res){
            // console.log(res);
            if(res.data){
                self.table.data = res.data.data ? res.data.data : [];
            }
        });
    },
    methods: {
        loadTable(){
            let self = this;
            self.table.loading = true;

            return new Promise(function(resolve,reject){
                self.$http
                    .get('/app/v1/toolRunLogs/batchNos',{
                        params: {
                            pageSize: 10,
                            currentPage: self.table.currentPage
                        }
                    })
                    .then(function(res) {
                        self.table.total = res.data.recordCount;
                        self.table.loading = false;
                        resolve(res);
                    },function(res){
                        self.table.loading = false;
                        reject(res);
                    });
            });
        },
        goDetail: function(p) {
            // console.log(p.row.name);
            this.$router.push({
                path: "/finalResult/scoreDetail",
                query: {
                    batchNo: p.row.batchNo,
                    scoreCardOid: p.row.scoreCardOid
                }
            });
        },
        search (data, argumentObj) {
            let res = data;
            let dataClone = data;
            for (let argu in argumentObj) {
                if (argumentObj[argu].length > 0) {
                    res = dataClone.filter(d => {
                        return d[argu].indexOf(argumentObj[argu]) > -1;
                    });
                    dataClone = res;
                }
            }
            return res;
        },
        handleSearch () {
            this.table.dataList = this.table.initTable;
            this.table.dataList = this.search(this.table.dataList, {name: this.product.model});
        }
    }
};
</script>
